﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace OMEGA.ControlLibrary
{
    /*****************************************************
     * 图片数字控制控件
     * 
     * 应用方法
     * 1.传递图片路径和初始化textbox上的count值
     * 2.利用MakeThumbnail初始化图片，然后放置到picturesBox里面
     * 
     * creat by OMEGA 
     * modify by vega li date：2009-11-23
     ******************************************************/
    internal partial class ImageCombo : UserControl
    {
        public ImageCombo()
        {
            InitializeComponent();
            this.txtFileName.TextAlign = HorizontalAlignment.Center;
        }

        #region 属性
        private string _ImagePath;
        public string ImagePath
        {
            get
            {
                return _ImagePath;
            }
            set
            {
                _ImagePath = value;
                this.pbTop.Image = MakeThumbnail(value, 150, 122, "H");                
            }
        }

        public int Count
        {
            get
            {
                int count = 0;
                int.TryParse(this.tbCount.Text, out count);
                if (count < 0)
                    return 0;
                return count;
            }
            set
            {
                this.tbCount.Text = value.ToString();
            }
        }


        private string _fileName;
        public string FileName
        {
            get { return _fileName; }
            set 
            {
                _fileName = value;
                txtFileName.Text = _fileName;
            }
        }
        #endregion 属性

        #region 方法
        public void InitView(ImageParams ImgParm)
        {
            ImagePath = ImgParm.ImagePath;
            Count = ImgParm.Count;
            FileName = ImgParm.FileName;
        }
        
        public static Image MakeThumbnail(string originalImagePath, int width, int height, string mode)
        {
            Image originalImage = Image.FromFile(originalImagePath);

            int towidth = width;
            int toheight = height;

            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

            switch (mode)
            {
                case "HW"://指定高宽缩放 
                    break;
                case "W"://指定宽，高按比例                    
                    toheight = originalImage.Height * width / originalImage.Width;
                    break;
                case "H"://指定高，宽按比例
                    towidth = originalImage.Width * height / originalImage.Height;
                    break;
                case "Cut"://指定高宽裁减
                    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                    {
                        oh = originalImage.Height;
                        ow = originalImage.Height * towidth / toheight;
                        y = 0;
                        x = (originalImage.Width - ow) / 2;
                    }
                    else
                    {
                        ow = originalImage.Width;
                        oh = originalImage.Width * height / towidth;
                        x = 0;
                        y = (originalImage.Height - oh) / 2;
                    }
                    break;
                default:
                    break;
            }

            //新建一个bmp图片
            Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

            //新建一个画板
            Graphics g = System.Drawing.Graphics.FromImage(bitmap);

            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充
            g.Clear(Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
                new Rectangle(x, y, ow, oh),
                GraphicsUnit.Pixel);

            return bitmap;
        }
        #endregion  方法

        #region 事件
        private void btnDic_Click(object sender, EventArgs e)
        {
            int count = 0;
            int.TryParse(this.tbCount.Text, out count);
            this.tbCount.Text = --count >= 0 ? count.ToString() : "0";
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            int count = 0;
            int.TryParse(this.tbCount.Text, out count);
            this.tbCount.Text = ++count >= 0 ? count.ToString() : "0";
        }
        #endregion 事件

    }
}
